package lily.phs.mental;

import lily.phs.object.HereTipa;
import lily.phs.object.MentalerInfo;
import lily.phs.urh.R;
import lily.ui.control.MyPopupWindow;
import android.content.Context;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.text.InputType;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;

public class MentalerInfoView extends LinearLayout implements OnClickListener, OnFocusChangeListener{

	private Context mContext;
	private EditText et_name,et_id,et_guardian_name,et_guardian_relation,et_guardian_address,et_guardian_phone,
	et_neighbor_name,et_neighbor_phone,et_self_signature,et_self_signature_time,et_first_occur_time,
	et_past_symptom_other,et_first_treat_time,et_hospital_times,et_current_state,et_diagnose_hospital,
	et_diagnose_time,et_influ_trouble,et_influ_accident,et_influ_disaster,et_influ_injure,et_influ_suicide,
	et_advices,et_date,et_doctor;
	
	private CheckBox cb_past_symptom_1,cb_past_symptom_2,cb_past_symptom_3,cb_past_symptom_4,cb_past_symptom_5,
	cb_past_symptom_6,cb_past_symptom_7,cb_past_symptom_8,cb_past_symptom_9,cb_past_symptom_10,
	cb_past_symptom_11,cb_past_symptom_12,cb_influence_1,cb_influence_2,cb_influence_3,cb_influence_4,
	cb_influence_5,cb_influence_6;
	
	private RadioGroup rg_approved,rg_clinic,rg_recent_treatment,rg_lock,rg_economy;
	private RadioButton rb_approved_1,rb_approved_2,rb_clinic_1,rb_clinic_2,rb_clinic_3,rb_recent_treatment_1,
	rb_recent_treatment_2,rb_recent_treatment_3,rb_recent_treatment_4,rb_lock_1,rb_lock_2,rb_lock_3,
	rb_economy_1,rb_economy_2,rb_economy_3;
	
	private MyPopupWindow mPopupWindow;
	
	private HereTipa ht;
	private MentalerInfo mInfo;
	private View child;
	private int profileId = 0;
	
	public MentalerInfoView(Context context, int profileid) {
		super(context);
		// TODO Auto-generated constructor stub
		this.mContext = context;
		this.profileId = profileid;
		
		final LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		child = inflater.inflate(R.layout.personal_info_plus, null);
		
		//这是第一个页面，在主线程中加载，但数据处理可放在次线程中
		findViewsById();
		setMyView();
	}
	
	/**
	 * 控件与xml文件关联
	 */
	public void findViewsById() {
		et_name = (EditText)child.findViewById(R.id.first_name);
		et_id = (EditText)child.findViewById(R.id.first_id);
		et_guardian_name = (EditText)child.findViewById(R.id.guardian_name);
		et_guardian_relation = (EditText)child.findViewById(R.id.guardian_relation);
		et_guardian_address = (EditText)child.findViewById(R.id.guardian_address);
		et_guardian_phone = (EditText)child.findViewById(R.id.guardian_phone);
		et_neighbor_name = (EditText)child.findViewById(R.id.neighbor_name);
		et_neighbor_phone = (EditText)child.findViewById(R.id.neighbor_phone);
		et_self_signature = (EditText)child.findViewById(R.id.self_signature);
		et_self_signature_time = (EditText)child.findViewById(R.id.self_signature_time);
		et_self_signature_time.setInputType(InputType.TYPE_NULL);
		et_self_signature_time.setOnClickListener(this);
		et_self_signature_time.setOnFocusChangeListener(this);
		et_first_occur_time = (EditText)child.findViewById(R.id.first_occur_time);
		et_first_occur_time.setInputType(InputType.TYPE_NULL);
		et_first_occur_time.setOnClickListener(this);
		et_first_occur_time.setOnFocusChangeListener(this);
		et_past_symptom_other = (EditText)child.findViewById(R.id.past_symptom_other);
		et_past_symptom_other.setOnClickListener(this);
		et_past_symptom_other.setOnFocusChangeListener(this);
		et_first_treat_time = (EditText)child.findViewById(R.id.first_treat_time);
		et_first_treat_time.setInputType(InputType.TYPE_NULL);
		et_first_treat_time.setOnClickListener(this);
		et_first_treat_time.setOnFocusChangeListener(this);
		et_hospital_times = (EditText)child.findViewById(R.id.hospital_times);
		et_current_state = (EditText)child.findViewById(R.id.current_state);
		et_diagnose_hospital = (EditText)child.findViewById(R.id.diagnose_hospital);
		et_diagnose_time = (EditText)child.findViewById(R.id.diagnose_time);
		et_diagnose_time.setInputType(InputType.TYPE_NULL);
		et_diagnose_time.setOnClickListener(this);
		et_diagnose_time.setOnFocusChangeListener(this);
		et_influ_trouble = (EditText)child.findViewById(R.id.influ_trouble);
		et_influ_trouble.setOnClickListener(this);
		et_influ_trouble.setOnFocusChangeListener(this);
		et_influ_accident = (EditText)child.findViewById(R.id.influ_accident);
		et_influ_accident.setOnClickListener(this);
		et_influ_accident.setOnFocusChangeListener(this);
		et_influ_disaster = (EditText)child.findViewById(R.id.influ_disaster);
		et_influ_disaster.setOnClickListener(this);
		et_influ_disaster.setOnFocusChangeListener(this);
		et_influ_injure = (EditText)child.findViewById(R.id.influ_injure);
		et_influ_injure.setOnClickListener(this);
		et_influ_injure.setOnFocusChangeListener(this);
		et_influ_suicide = (EditText)child.findViewById(R.id.influ_suicide);
		et_influ_suicide.setOnClickListener(this);
		et_influ_suicide.setOnFocusChangeListener(this);
		et_advices = (EditText)child.findViewById(R.id.advices);
		et_date = (EditText)child.findViewById(R.id.date);
		et_date.setInputType(InputType.TYPE_NULL);
		et_date.setOnClickListener(this);
		et_date.setOnFocusChangeListener(this);
		et_doctor = (EditText)child.findViewById(R.id.doctor);
		cb_past_symptom_1 = (CheckBox)child.findViewById(R.id.past_symptom_1);
		cb_past_symptom_2 = (CheckBox)child.findViewById(R.id.past_symptom_2);
		cb_past_symptom_3 = (CheckBox)child.findViewById(R.id.past_symptom_3);
		cb_past_symptom_4 = (CheckBox)child.findViewById(R.id.past_symptom_4);
		cb_past_symptom_5 = (CheckBox)child.findViewById(R.id.past_symptom_5);
		cb_past_symptom_6 = (CheckBox)child.findViewById(R.id.past_symptom_6);
		cb_past_symptom_7 = (CheckBox)child.findViewById(R.id.past_symptom_7);
		cb_past_symptom_8 = (CheckBox)child.findViewById(R.id.past_symptom_8);
		cb_past_symptom_9 = (CheckBox)child.findViewById(R.id.past_symptom_9);
		cb_past_symptom_10 = (CheckBox)child.findViewById(R.id.past_symptom_10);
		cb_past_symptom_11 = (CheckBox)child.findViewById(R.id.past_symptom_11);
		cb_past_symptom_12 = (CheckBox)child.findViewById(R.id.past_symptom_12);
		cb_influence_1 = (CheckBox)child.findViewById(R.id.influence_1);
		cb_influence_2 = (CheckBox)child.findViewById(R.id.influence_2);
		cb_influence_3 = (CheckBox)child.findViewById(R.id.influence_3);
		cb_influence_4 = (CheckBox)child.findViewById(R.id.influence_4);
		cb_influence_5 = (CheckBox)child.findViewById(R.id.influence_5);
		cb_influence_6 = (CheckBox)child.findViewById(R.id.influence_6);
		rg_approved = (RadioGroup)child.findViewById(R.id.approved);
		rg_clinic = (RadioGroup)child.findViewById(R.id.clinic);
		rg_recent_treatment = (RadioGroup)child.findViewById(R.id.recent_treatment);
		rg_lock = (RadioGroup)child.findViewById(R.id.lock);
		rg_economy = (RadioGroup)child.findViewById(R.id.economy);
		rb_approved_1 = (RadioButton)child.findViewById(R.id.approved_1);
		rb_approved_2 = (RadioButton)child.findViewById(R.id.approved_2);
		rb_clinic_1 = (RadioButton)child.findViewById(R.id.clinic_1);
		rb_clinic_2 = (RadioButton)child.findViewById(R.id.clinic_2);
		rb_clinic_3 = (RadioButton)child.findViewById(R.id.clinic_3);
		rb_recent_treatment_1 = (RadioButton)child.findViewById(R.id.recent_treatment_1);
		rb_recent_treatment_2 = (RadioButton)child.findViewById(R.id.recent_treatment_2);
		rb_recent_treatment_3 = (RadioButton)child.findViewById(R.id.recent_treatment_3);
		rb_recent_treatment_4 = (RadioButton)child.findViewById(R.id.recent_treatment_4);
		rb_lock_1 = (RadioButton)child.findViewById(R.id.lock_1);
		rb_lock_2 = (RadioButton)child.findViewById(R.id.lock_2);
		rb_lock_3 = (RadioButton)child.findViewById(R.id.lock_3);
		rb_economy_1 = (RadioButton)child.findViewById(R.id.economy_1);
		rb_economy_2 = (RadioButton)child.findViewById(R.id.economy_2);
		rb_economy_3 = (RadioButton)child.findViewById(R.id.economy_3);
	}
	
	/**
	 * 初始化views
	 */
	public void initViews() {
		// TODO Auto-generated method stub
		initialState();
		
		String temp = "";
		int itemp = -1;
		
		temp = ht.getHeti_name();
		if(temp!=null){
			et_name.setText(temp);
		}
		temp = ht.getHeti_here_id();
		if(temp!=null){
			et_id.setText(temp);
		}
		et_name.setEnabled(false);
		et_id.setEnabled(false);
		
		temp = mInfo.getMein_guardian_name();
		if(temp.length()>0){
			et_guardian_name.setText(temp);
		}
		temp = mInfo.getMein_guardian_relation();
		if(temp.length()>0){
			et_guardian_relation.setText(temp);
		}
		temp = mInfo.getMein_guardian_address();
		if(temp.length()>0){
			et_guardian_address.setText(temp);
		}
		temp = mInfo.getMein_guardian_phone();
		if(temp.length()>0){
			et_guardian_phone.setText(temp);
		}
		temp = mInfo.getMein_neighbor_name();
		if(temp.length()>0){
			et_neighbor_name.setText(temp);
		}
		temp = mInfo.getMein_neighbor_phone();
		if(temp.length()>0){
			et_neighbor_phone.setText(temp);
		}
		
		itemp = mInfo.getMein_approved();
		switch(itemp){
		case 1:{
			rg_approved.check(0);
			rb_approved_1.setChecked(true);
			break;
		}
		case 0:{
			rg_approved.check(1);
			rb_approved_2.setChecked(true);
			break;
		}
		}
		temp = mInfo.getMein_self_signature();
		if(temp.length()>0){
			et_self_signature.setText(temp);
		}
		temp = mInfo.getMein_self_signature_time();
		if(temp.length()>0){
			et_self_signature_time.setText(temp);
		}
		temp = mInfo.getMein_first_occur_time();
		if(temp.length()>0){
			et_first_occur_time.setText(temp);
		}
		
		temp = mInfo.getMein_past_symptoms();
		if(temp.length()>0){
			String[] ss = temp.split(",");
			for(int i=0;i<ss.length;i++){
				int k = Integer.parseInt(ss[i]);
				switch(k){
				case 1:{
					cb_past_symptom_1.setChecked(true);
					break;
				}
				case 2:{
					cb_past_symptom_2.setChecked(true);
					break;
				}
				case 3:{
					cb_past_symptom_3.setChecked(true);
					break;
				}
				case 4:{
					cb_past_symptom_4.setChecked(true);
					break;
				}
				case 5:{
					cb_past_symptom_5.setChecked(true);
					break;
				}
				case 6:{
					cb_past_symptom_6.setChecked(true);
					break;
				}
				case 7:{
					cb_past_symptom_7.setChecked(true);
					break;
				}
				case 8:{
					cb_past_symptom_8.setChecked(true);
					break;
				}
				case 9:{
					cb_past_symptom_9.setChecked(true);
					break;
				}
				case 10:{
					cb_past_symptom_10.setChecked(true);
					break;
				}
				case 11:{
					cb_past_symptom_11.setChecked(true);
					break;
				}
				case 12:{
					cb_past_symptom_12.setChecked(true);
					temp = mInfo.getMein_past_symptoms_other();
					et_past_symptom_other.setText(temp);
					break;
				}
				}
			}
		}
		
		itemp = mInfo.getMein_clinic();
		switch(itemp){
		case 1:
			rg_clinic.check(0);
			rb_clinic_1.setChecked(true);
			break;
		case 2:
			rg_clinic.check(1);
			rb_clinic_2.setChecked(true);
			break;
		case 3:
			rg_clinic.check(2);
			rb_clinic_3.setChecked(true);
			break;
		}
		temp = mInfo.getMein_first_treat_time();
		if(temp.length()>0){
			et_first_treat_time.setText(temp);
		}
		itemp = mInfo.getMein_hospital_times();
		if(itemp!=-1){
			et_hospital_times.setText(String.valueOf(itemp));
		}
		temp = mInfo.getMein_current_state();
		if(temp.length()>0){
			et_current_state.setText(temp);
		}
		temp = mInfo.getMein_diagnose_hospital();
		if(temp.length()>0){
			et_diagnose_hospital.setText(temp);
		}
		temp = mInfo.getMein_diagnose_time();
		if(temp.length()>0){
			et_diagnose_time.setText(temp);
		}
		
		itemp = mInfo.getMein_recent_treatment();
		switch(itemp){
		case 1:
			rg_recent_treatment.check(0);
			rb_recent_treatment_1.setChecked(true);
			break;
		case 2:
			rg_recent_treatment.check(1);
			rb_recent_treatment_2.setChecked(true);
			break;
		case 3:
			rg_recent_treatment.check(2);
			rb_recent_treatment_3.setChecked(true);
			break;
		case 4:
			rg_recent_treatment.check(3);
			rb_recent_treatment_4.setChecked(true);
			break;
		}
		
		temp = mInfo.getMein_influence();
		if(temp.length()>0){
			String[] ss = temp.split(",");
			for(int i=0;i<ss.length;i++){
				int k = Integer.valueOf(ss[i]);
				switch(k){
				case 1:{
					cb_influence_1.setChecked(true);
					itemp = mInfo.getMein_influ_trouble();
					if(itemp!=-1){
						et_influ_trouble.setText(String.valueOf(itemp));
					}
					break;
				}
				case 2:{
					cb_influence_2.setChecked(true);
					itemp = mInfo.getMein_influ_accident();
					if(itemp!=-1){
						et_influ_accident.setText(String.valueOf(itemp));
					}
					break;
				}
				case 3:{
					cb_influence_3.setChecked(true);
					itemp = mInfo.getMein_influ_disaster();
					if(itemp!=-1){
						et_influ_disaster.setText(String.valueOf(itemp));
					}
					break;
				}
				case 4:{
					cb_influence_4.setChecked(true);
					itemp = mInfo.getMein_influ_injure();
					if(itemp!=-1){
						et_influ_injure.setText(String.valueOf(itemp));
					}
					break;
				}
				case 5:{
					cb_influence_5.setChecked(true);
					itemp = mInfo.getMein_influ_suicide();
					if(itemp!=-1){
						et_influ_suicide.setText(String.valueOf(itemp));
					}
					break;
				}
				case 6:{
					cb_influence_6.setChecked(true);
					break;
				}
				}
				
			}
		}
		
		itemp = mInfo.getMein_lock();
		switch(itemp){
		case 1:
			rg_lock.check(0);
			rb_lock_1.setChecked(true);
			break;
		case 2:
			rg_lock.check(1);
			rb_lock_2.setChecked(true);
			break;
		case 3:
			rg_lock.check(2);
			rb_lock_3.setChecked(true);
			break;
		}
		itemp = mInfo.getMein_economy();
		switch(itemp){
		case 1:
			rg_economy.check(0);
			rb_economy_1.setChecked(true);
			break;
		case 2:
			rg_economy.check(1);
			rb_economy_2.setChecked(true);
			break;
		case 3:
			rg_economy.check(2);
			rb_economy_3.setChecked(true);
			break;
		}
		temp = mInfo.getMein_advices();
		if(temp.length()>0){
			et_advices.setText(temp);
		}
		temp = mInfo.getMein_date();
		if(temp.length()>0){
			et_date.setText(temp);
		}
		temp = mInfo.getMein_doctor();
		if(temp.length()>0){
			et_doctor.setText(temp);
		}
		
	}
	
	/**
	 * 初始时状态：横线为空，所有选择项未选中
	 */
	private void initialState() {
		et_name.setText("");
		et_id.setText("");
		et_guardian_name.setText("");
		et_guardian_relation.setText("");
		et_guardian_address.setText("");
		et_guardian_phone.setText("");
		et_neighbor_name.setText("");
		et_neighbor_phone.setText("");
		et_self_signature.setText("");
		et_self_signature_time.setText("");
		et_first_occur_time.setText("");
		et_past_symptom_other.setText("");
		et_first_treat_time.setText("");
		et_hospital_times.setText("");
		et_current_state.setText("");
		et_diagnose_hospital.setText("");
		et_diagnose_time.setText("");
		et_influ_trouble.setText("");
		et_influ_accident.setText("");
		et_influ_disaster.setText("");
		et_influ_injure.setText("");
		et_influ_suicide.setText("");
		et_advices.setText("");
		et_date.setText("");
		et_doctor.setText("");
		cb_past_symptom_1.setChecked(false);
		cb_past_symptom_2.setChecked(false);
		cb_past_symptom_3.setChecked(false);
		cb_past_symptom_4.setChecked(false);
		cb_past_symptom_5.setChecked(false);
		cb_past_symptom_6.setChecked(false);
		cb_past_symptom_7.setChecked(false);
		cb_past_symptom_8.setChecked(false);
		cb_past_symptom_9.setChecked(false);
		cb_past_symptom_10.setChecked(false);
		cb_past_symptom_11.setChecked(false);
		cb_past_symptom_12.setChecked(false);
		cb_influence_1.setChecked(false);
		cb_influence_2.setChecked(false);
		cb_influence_3.setChecked(false);
		cb_influence_4.setChecked(false);
		cb_influence_5.setChecked(false);
		cb_influence_6.setChecked(false);
		rb_approved_1.setChecked(false);
		rb_approved_2.setChecked(false);
		rb_clinic_1.setChecked(false);
		rb_clinic_2.setChecked(false);
		rb_clinic_3.setChecked(false);
		rb_recent_treatment_1.setChecked(false);
		rb_recent_treatment_2.setChecked(false);
		rb_recent_treatment_3.setChecked(false);
		rb_recent_treatment_4.setChecked(false);
		rb_lock_1.setChecked(false);
		rb_lock_2.setChecked(false);
		rb_lock_3.setChecked(false);
		rb_economy_1.setChecked(false);
		rb_economy_2.setChecked(false);
		rb_economy_3.setChecked(false);
	}
	
	/**
	 * 从数据库中检出数据
	 */
	public void fetchData(){
		ht = new HereTipa(mContext);
		ht.selectNameAndCode(profileId);
		
		mInfo = new MentalerInfo(mContext);
//		mInfo.setMein_profileid(profileId);
		mInfo.selectMentalerInfo(profileId);
	}
	
	/**
	 * 将view添加到父view中
	 */
	public void setMyView(){
		this.removeAllViews();
		LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);
		this.addView(child,lp);
	}
	
	/**
	 * 将整个界面作为一个view对象
	 * @return 整个界面view
	 */
	public View getMyView(){
		return child;
	}
	
	/**
	 * 保存views的值到数据库
	 */
	public void saveViews() {
		MentalerInfo mi = new MentalerInfo(mContext);
		mi.selectMentalerInfo(profileId);
		
		String temp = "";
		StringBuffer sb;
		
		temp = et_guardian_name.getText().toString();
		if(temp.length()>0){
			mi.setMein_guardian_name(temp);
		}
		temp = et_guardian_relation.getText().toString();
		if(temp.length()>0){
			mi.setMein_guardian_relation(temp);
		}
		temp = et_guardian_address.getText().toString();
		if(temp.length()>0){
			mi.setMein_guardian_address(temp);
		}
		temp = et_guardian_phone.getText().toString();
		if(temp.length()>0){
			mi.setMein_guardian_phone(temp);
		}
		temp = et_neighbor_name.getText().toString();
		if(temp.length()>0){
			mi.setMein_neighbor_name(temp);
		}
		temp = et_neighbor_phone.getText().toString();
		if(temp.length()>0){
			mi.setMein_neighbor_phone(temp);
		}
		
		if(rb_approved_1.isChecked()==true){
			mi.setMein_approved(1);
		}else if(rb_approved_2.isChecked()==true){
			mi.setMein_approved(0);
		}
		
		temp = et_self_signature.getText().toString();
		if(temp.length()>0){
			mi.setMein_self_signature(temp);
		}
		temp = et_self_signature_time.getText().toString();
		if(temp.length()>0){
			mi.setMein_self_signature_time(temp);
		}
		temp = et_first_occur_time.getText().toString();
		if(temp.length()>0){
			mi.setMein_first_occur_time(temp);
		}
		
		sb = new StringBuffer();
		if(cb_past_symptom_1.isChecked()==true){
			sb.append("1,");
		}
		if(cb_past_symptom_2.isChecked()==true){
			sb.append("2,");
		}
		if(cb_past_symptom_3.isChecked()==true){
			sb.append("3,");
		}
		if(cb_past_symptom_4.isChecked()==true){
			sb.append("4,");
		}
		if(cb_past_symptom_5.isChecked()==true){
			sb.append("5,");
		}
		if(cb_past_symptom_6.isChecked()==true){
			sb.append("6,");
		}
		if(cb_past_symptom_7.isChecked()==true){
			sb.append("7,");
		}
		if(cb_past_symptom_8.isChecked()==true){
			sb.append("8,");
		}
		if(cb_past_symptom_9.isChecked()==true){
			sb.append("9,");
		}
		if(cb_past_symptom_10.isChecked()==true){
			sb.append("10,");
		}
		if(cb_past_symptom_11.isChecked()==true){
			sb.append("11,");
		}
		if(cb_past_symptom_12.isChecked()==true){
			sb.append("12,");
			temp = et_past_symptom_other.getText().toString();
			mi.setMein_past_symptoms_other(temp);
		}
		mi.setMein_past_symptoms(sb.toString());
		
		if(rb_clinic_1.isChecked()==true){
			mi.setMein_clinic(1);
		}else if(rb_clinic_2.isChecked()==true){
			mi.setMein_clinic(2);
		}else if(rb_clinic_3.isChecked()==true){
			mi.setMein_clinic(3);
		}
		
		temp = et_first_treat_time.getText().toString();
		if(temp.length()>0){
			mi.setMein_first_treat_time(temp);
		}
		temp = et_hospital_times.getText().toString();
		if(temp.length()>0){
			mi.setMein_hospital_times(Integer.valueOf(temp));
		}
		temp = et_current_state.getText().toString();
		if(temp.length()>0){
			mi.setMein_current_state(temp);
		}
		temp = et_diagnose_hospital.getText().toString();
		if(temp.length()>0){
			mi.setMein_diagnose_hospital(temp);
		}
		temp = et_diagnose_time.getText().toString();
		if(temp.length()>0){
			mi.setMein_diagnose_time(temp);
		}
		
		if(rb_recent_treatment_1.isChecked()==true){
			mi.setMein_recent_treatment(1);
		}else if(rb_recent_treatment_2.isChecked()==true){
			mi.setMein_recent_treatment(2);
		}else if(rb_recent_treatment_3.isChecked()==true){
			mi.setMein_recent_treatment(3);
		}else if(rb_recent_treatment_4.isChecked()==true){
			mi.setMein_recent_treatment(4);
		}
		
		sb = new StringBuffer();
		if(cb_influence_1.isChecked()==true){
			sb.append("1,");
			temp = et_influ_trouble.getText().toString();
			if(temp.length()>0){
				mi.setMein_influ_trouble(Integer.parseInt(temp));
			}
		}
		if(cb_influence_2.isChecked()==true){
			sb.append("2,");
			temp = et_influ_accident.getText().toString();
			if(temp.length()>0){
				mi.setMein_influ_accident(Integer.parseInt(temp));
			}
		}
		if(cb_influence_3.isChecked()==true){
			sb.append("3,");
			temp = et_influ_disaster.getText().toString();
			if(temp.length()>0){
				mi.setMein_influ_disaster(Integer.parseInt(temp));
			}
		}
		if(cb_influence_4.isChecked()==true){
			sb.append("4,");
			temp = et_influ_injure.getText().toString();
			if(temp.length()>0){
				mi.setMein_influ_injure(Integer.parseInt(temp));
			}
		}
		if(cb_influence_5.isChecked()==true){
			sb.append("5,");
			temp = et_influ_suicide.getText().toString();
			if(temp.length()>0){
				mi.setMein_influ_suicide(Integer.parseInt(temp));
			}
		}
		if(cb_influence_6.isChecked()==true){
			sb.append("6,");
		}
		mi.setMein_influence(sb.toString());
		
		if(rb_lock_1.isChecked()==true){
			mi.setMein_lock(1);
		}else if(rb_lock_2.isChecked()==true){
			mi.setMein_lock(2);
		}else if(rb_lock_3.isChecked()==true){
			mi.setMein_lock(3);
		}
		if(rb_economy_1.isChecked()==true){
			mi.setMein_economy(1);
		}else if(rb_economy_2.isChecked()==true){
			mi.setMein_economy(2);
		}else if(rb_economy_3.isChecked()==true){
			mi.setMein_economy(3);
		}
		
		temp = et_advices.getText().toString();
		if(temp.length()>0){
			mi.setMein_advices(temp);
		}
		temp = et_date.getText().toString();
		if(temp.length()>0){
			mi.setMein_date(temp);
		}
		temp = et_doctor.getText().toString();
		if(temp.length()>0){
			mi.setMein_doctor(temp);
		}
		
		mi.updateMentalerInfo(profileId);
	}
	
	@Override
	public void onFocusChange(View v, boolean hasFocus) {
		// TODO Auto-generated method stub
		if(v==et_self_signature_time&&hasFocus){
			mPopupWindow = new MyPopupWindow(mContext);			
			mPopupWindow.showAtLocation(v.findViewById(R.id.self_signature_time), Gravity.CENTER, 0, 0);

			Button btn=mPopupWindow.getButton();
			btn.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					et_self_signature_time.setText(mPopupWindow.getDate());
					mPopupWindow.dismiss();
				}
				
			});
		}
		else if(v==et_first_occur_time&&hasFocus){
			mPopupWindow = new MyPopupWindow(mContext);			
			mPopupWindow.showAtLocation(v.findViewById(R.id.first_occur_time), Gravity.CENTER, 0, 0);

			Button btn=mPopupWindow.getButton();
			btn.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					et_first_occur_time.setText(mPopupWindow.getDate());
					mPopupWindow.dismiss();
				}
				
			});
		}
		else if(v==et_first_treat_time&&hasFocus){
			mPopupWindow = new MyPopupWindow(mContext);			
			mPopupWindow.showAtLocation(v.findViewById(R.id.first_treat_time), Gravity.CENTER, 0, 0);

			Button btn=mPopupWindow.getButton();
			btn.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					et_first_treat_time.setText(mPopupWindow.getDate());
					mPopupWindow.dismiss();
				}
				
			});
		}
		else if(v==et_diagnose_time&&hasFocus){
			mPopupWindow = new MyPopupWindow(mContext);			
			mPopupWindow.showAtLocation(v.findViewById(R.id.diagnose_time), Gravity.CENTER, 0, 0);

			Button btn=mPopupWindow.getButton();
			btn.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					et_diagnose_time.setText(mPopupWindow.getDate());
					mPopupWindow.dismiss();
				}
				
			});
		}
		else if(v==et_date&&hasFocus){
			mPopupWindow = new MyPopupWindow(mContext);			
			mPopupWindow.showAtLocation(v.findViewById(R.id.date), Gravity.CENTER, 0, 0);

			Button btn=mPopupWindow.getButton();
			btn.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					et_date.setText(mPopupWindow.getDate());
					mPopupWindow.dismiss();
				}
				
			});
		}
		else if(v==et_past_symptom_other&&hasFocus){
			cb_past_symptom_12.setChecked(true);
		}
		else if(v==et_influ_trouble&&hasFocus){
			cb_influence_1.setChecked(true);
		}
		else if(v==et_influ_accident&&hasFocus){
			cb_influence_2.setChecked(true);
		}
		else if(v==et_influ_disaster&&hasFocus){
			cb_influence_3.setChecked(true);
		}
		else if(v==et_influ_injure&&hasFocus){
			cb_influence_4.setChecked(true);
		}
		else if(v==et_influ_suicide&&hasFocus){
			cb_influence_5.setChecked(true);
		}
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if(v==et_self_signature_time){
			mPopupWindow = new MyPopupWindow(mContext);			
			mPopupWindow.showAtLocation(v.findViewById(R.id.self_signature_time), Gravity.CENTER, 0, 0);

			Button btn=mPopupWindow.getButton();
			btn.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					et_self_signature_time.setText(mPopupWindow.getDate());
					mPopupWindow.dismiss();
				}
				
			});
		}
		else if(v==et_first_occur_time){
			mPopupWindow = new MyPopupWindow(mContext);			
			mPopupWindow.showAtLocation(v.findViewById(R.id.first_occur_time), Gravity.CENTER, 0, 0);

			Button btn=mPopupWindow.getButton();
			btn.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					et_first_occur_time.setText(mPopupWindow.getDate());
					mPopupWindow.dismiss();
				}
				
			});
		}
		else if(v==et_first_treat_time){
			mPopupWindow = new MyPopupWindow(mContext);			
			mPopupWindow.showAtLocation(v.findViewById(R.id.first_treat_time), Gravity.CENTER, 0, 0);

			Button btn=mPopupWindow.getButton();
			btn.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					et_first_treat_time.setText(mPopupWindow.getDate());
					mPopupWindow.dismiss();
				}
				
			});
		}
		else if(v==et_diagnose_time){
			mPopupWindow = new MyPopupWindow(mContext);			
			mPopupWindow.showAtLocation(v.findViewById(R.id.diagnose_time), Gravity.CENTER, 0, 0);

			Button btn=mPopupWindow.getButton();
			btn.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					et_diagnose_time.setText(mPopupWindow.getDate());
					mPopupWindow.dismiss();
				}
				
			});
		}
		else if(v==et_date){
			mPopupWindow = new MyPopupWindow(mContext);			
			mPopupWindow.showAtLocation(v.findViewById(R.id.date), Gravity.CENTER, 0, 0);

			Button btn=mPopupWindow.getButton();
			btn.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					et_date.setText(mPopupWindow.getDate());
					mPopupWindow.dismiss();
				}
				
			});
		}
		else if(v==et_past_symptom_other){
			cb_past_symptom_12.setChecked(true);
		}
		else if(v==et_influ_trouble){
			cb_influence_1.setChecked(true);
		}
		else if(v==et_influ_accident){
			cb_influence_2.setChecked(true);
		}
		else if(v==et_influ_disaster){
			cb_influence_3.setChecked(true);
		}
		else if(v==et_influ_injure){
			cb_influence_4.setChecked(true);
		}
		else if(v==et_influ_suicide){
			cb_influence_5.setChecked(true);
		}
	}

}
